package no.group.gowther.service.dto.param;

import no.group.gowther.exception.BusinessException;
import no.group.gowther.exception.ErrorMessage;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @author ayatsuji
 * @date 2022-04-18 21:43
 * @since
 */
public class Page {
    private static final int DEFAULT_PAGE_NUM = 1;
    private static final int DEFAULT_PAGE_Size = 15;


    @NotNull(message = "pageNum不能为空")
    @Min(value = 1, message = "pageNum不能小于1")
    private Integer pageNum = DEFAULT_PAGE_NUM;
    @NotNull(message = "pageSize不能为空")
    @Min(value = 1, message = "pageSize不能小于1")
    private Integer pageSize = DEFAULT_PAGE_Size;

    public Page() {
    }

    public Page(int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        check();
    }
    private void check() {
        if (pageNum <= 0 || pageSize <= 0) {
            throw BusinessException.createException(ErrorMessage.PARAM_ERROR.getErrorCode(), "分页参数有误");
        }
    }

    public Page(Page page) {
        this(page.getPageNum(), page.getPageSize());
    }

    public int toPageOffset() {
        return (pageNum - 1) * pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                '}';
    }
}